package com.ruoyi.comparison.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.helper.LoginHelper;
import com.ruoyi.common.utils.BeanCopyUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.comparison.domain.TaskRelation;
import com.ruoyi.comparison.domain.TaskRelationList;
import com.ruoyi.comparison.domain.TaskSubjectList;
import com.ruoyi.comparison.domain.bo.TaskRelationListBo;
import com.ruoyi.comparison.domain.vo.TaskRelationListVo;
import com.ruoyi.comparison.domain.vo.TaskSubjectListVo;
import com.ruoyi.comparison.mapper.TaskRelationListMapper;
import com.ruoyi.comparison.service.ITaskRelationListService;
import com.ruoyi.comparison.service.ITaskSubjectListService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 同一条关联关系上的节点信息Service业务层处理
 *
 * @author ruoyi
 * @date 2023-11-18
 */
@RequiredArgsConstructor
@Service
public class TaskRelationListServiceImpl implements ITaskRelationListService {

    private final TaskRelationListMapper baseMapper;
    private final ITaskSubjectListService subjectListService;

    /**
     * 查询同一条关联关系上的节点信息
     */
    @Override
    public TaskRelationListVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询同一条关联关系上的节点信息列表
     */
    @Override
    public TableDataInfo<TaskRelation> queryPageList(TaskRelationListBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<TaskRelationList> lqw = buildQueryWrapper(bo);
        lqw.eq(TaskRelationList::getSubjectId,bo.getSubjectId());
        Page<TaskRelationListVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);

        List<TaskRelationListVo> records = result.getRecords();
        List<TaskRelation> list = new ArrayList<>();
        for (TaskRelationListVo record : records) {
            TaskSubjectList startSubjectList = subjectListService.findOne(record.getStartNodeId());
            TaskSubjectList endSubjectList = subjectListService.findOne(record.getEndNodeId());
            TaskRelation relation = BeanCopyUtils.copy(record, TaskRelation.class);
            assert relation != null;
            relation.setStartNodeName(startSubjectList.getName());
            relation.setSearchKey1(startSubjectList.getSearchKey1());
            relation.setSearchKey2(startSubjectList.getSearchKey2());
            relation.setEndNodeName(endSubjectList.getName());
            list.add(relation);
        }
        Page<TaskRelation> relationPage = new Page<>();
        relationPage.setRecords(list);
        relationPage.setPages(result.getPages());
        relationPage.setSize(result.getSize());
        relationPage.setTotal(result.getTotal());
        return TableDataInfo.build(relationPage);
    }

    /**
     * 查询同一条关联关系上的节点信息列表
     */
    @Override
    public List<TaskRelationListVo> queryList(TaskRelationListBo bo) {
        LambdaQueryWrapper<TaskRelationList> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<TaskRelationList> buildQueryWrapper(TaskRelationListBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<TaskRelationList> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getStartNodeId()), TaskRelationList::getStartNodeId, bo.getStartNodeId());
        lqw.eq(StringUtils.isNotBlank(bo.getEndNodeId()), TaskRelationList::getEndNodeId, bo.getEndNodeId());
        lqw.eq(StringUtils.isNotBlank(bo.getType()), TaskRelationList::getType, bo.getType());
        lqw.eq(StringUtils.isNotBlank(bo.getLabelText()), TaskRelationList::getLabelText, bo.getLabelText());
        return lqw;
    }

    /**
     * 新增同一条关联关系上的节点信息
     */
    @Override
    public Boolean insertByBo(TaskRelationListBo bo) {
        TaskRelationList add = BeanUtil.toBean(bo, TaskRelationList.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改同一条关联关系上的节点信息
     */
    @Override
    public Boolean updateByBo(TaskRelationListBo bo) {
        TaskRelationList update = BeanUtil.toBean(bo, TaskRelationList.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(TaskRelationList entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除同一条关联关系上的节点信息
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    public void insertBatch(List<TaskRelationList> list2) {
        this.baseMapper.insertBatch(list2);
    }
}
